package com.ln.cmsln.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.ln.cmsln.service.FeilService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

@Api(tags = "阿里云文件管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/api/oss/file")
public class FileController {
    @Resource
    private FeilService fileService;
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Object upload(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile file
    ) throws IOException {
        String module = "ln-oa-file" ;
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String upload = fileService.upload(inputStream, module, originalFilename);
        String code = "0" ;
        HashMap<Object, Object> map = new HashMap<>();
        if (upload.length()>0){
            code = "1" ;
        }else {
            code = "0" ;
        }
        map.put("code",code);
        map.put("msg",upload);
        String json = JSON.toJSONString(map);
        System.out.println("文件上传:upload = " + upload);
        return json ;
    }


}
